using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using System.Windows.Controls;
using _3DHouse.CustomControl;

namespace _3DHouse.Lib
{
    /// <summary>
    /// 旋转的撤消重做控制类
    /// </summary>
    class RotateCommand : ICommand
    {
        /// <summary>
        /// 当前对象
        /// </summary>
        private Cube _UiElement;
       // private RotateTransform3D _Rotate;
        /// <summary>
        /// 旋转角度
        /// </summary>
        double _Angle;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="element">旋转对象</param>
        /// <param name="angle">旋转角度</param>
        public RotateCommand(Cube element, double angle)
        {
            _Angle = angle;
            _UiElement = element;
            //SetRotateX();
        }

        #region ICommand 成员
        /// <summary>
        /// 重做
        /// </summary>
        public void Execute()
        {
            Transform3DGroup tfg = _UiElement.Transform as Transform3DGroup;
            foreach (Transform3D tr in tfg.Children)
            {
                if (tr.GetType().Name.Equals("RotateTransform3D"))
                {
                    RotateTransform3D _Rotate = tr as RotateTransform3D;
                    AxisAngleRotation3D axi = _Rotate.Rotation as AxisAngleRotation3D;
                    axi.Angle += _Angle;//重做会去
                }
            }
            _UiElement.Transform = tfg;
        }
        /// <summary>
        /// 撤消
        /// </summary>
        public void UnExecute()
        {
            Transform3DGroup tfg = _UiElement.Transform as Transform3DGroup;
            foreach (Transform3D tr in tfg.Children)
            {
                if (tr.GetType().Name.Equals("RotateTransform3D"))
                {
                    RotateTransform3D _Rotate = tr as RotateTransform3D;
                    AxisAngleRotation3D axi = _Rotate.Rotation as AxisAngleRotation3D;
                    axi.Angle -= _Angle; //减去角度撤消到旋转前                
                }
            }
            _UiElement.Transform = tfg;
        }

        #endregion
        /// <summary>
        /// 设置左右旋转
        /// </summary>
        /// <param name="n">平移值double类型</param>
        //private void SetRotateX()
        //{
        //    if (_UiElement != null)
        //    {               
        //        //组合变换
        //        if (_UiElement != null && _UiElement.Transform != null && _UiElement.Transform.GetType().Name.Equals("Transform3DGroup"))
        //        {
        //             Transform3DGroup tfg = _UiElement.Transform as Transform3DGroup;                  
                  
        //            foreach (Transform3D tr in tfg.Children)
        //            {
        //                if (tr.GetType().Name.Equals("RotateTransform3D"))
        //                {
        //                    RotateTransform3D _Rotate = tr as RotateTransform3D;                     
        //                }
        //            }
                   
        //        }
                
        //    }
        //}
    }
}
